﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Web;
using OPM.Library.Models;
using OPM.Models;
using OPM.Models.BaseModel;
using OPM.ServiceOfficer;
using OPM.ServiceGeneral;
using OPM.ServiceListOfValues;
using OPM.ServiceManageLOV;
using OPM.ServiceMember;
using System.Net;

namespace OPM.Class
{
    // Update 2015-05-04
    // Data Access Layer
    public static class WSG
    {
        #region General Service

        public static string EncodeString(string string_to_encode)
        {
            var s = new GeneralSoapClient();
            string resultStr = Newtonsoft.Json.JsonConvert.DeserializeObject(s.EncodeString(string_to_encode)).ToString();
            return resultStr;
        }

        public static OpmDetail GetOPMDetails()
        {
            var s = new GeneralSoapClient();
            var resultStr = s.GetOPMDetails();
            OpmDetail result = Newtonsoft.Json.JsonConvert.DeserializeObject<OpmDetail>(resultStr);
            return result;
        }

        #endregion
    }

    public static class WSL
    {
        #region ListOfValues Service

        public static CaseObjective[] GetCaseObjective()
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetCaseObjective(U.IsThai);
            CaseObjective[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<CaseObjective[]>(resultStr);
            return result;
        }

        public static CaseStatus[] GetCaseStatus()
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetCaseStatus(U.IsThai);
            CaseStatus[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<CaseStatus[]>(resultStr);
            return result;
        }


        public static CaseTerminalType[] GetCaseTerminalType(string org_id, string ref_id)
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetCaseTerminalType(org_id, ref_id, U.IsThai);
            CaseTerminalType[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<CaseTerminalType[]>(resultStr);
            return result;
        }
        public static List<string> CasetTypeIds = new List<string>();

        public static CaseType[] GetCaseType(string ref_id, bool getChild = false)
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetCaseType(ref_id, U.IsThai);
            CaseType[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<CaseType[]>(resultStr);

            if (getChild)
            {
                foreach (var c in result)
                {
                    CasetTypeIds.Add(c.val);
                    c.Child = new List<CaseType>();
                    c.Child.AddRange(GetCaseType(c.val, U.IsThai));
                }
            }

            return result;
        }

        public static CaseTypeOrgOwner[] GetCaseTypeOrgOwner(string case_type_id)
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetCaseTypeOrgOwner(case_type_id, U.IsThai);
            CaseTypeOrgOwner[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<CaseTypeOrgOwner[]>(resultStr);
            return result;
        }

        public static ChannelIn[] GetChannelIn(string ref_id, string org_id)
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetChannelIn(ref_id, org_id, U.IsThai);
            ChannelIn[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<ChannelIn[]>(resultStr);
            return result;
        }

        public static ChannelOut[] GetChannelOut(string ref_id)
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetChannelOut(ref_id, U.IsThai);
            ChannelOut[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<ChannelOut[]>(resultStr);
            return result;
        }

        public static ContactObjective[] GetContactObjective(string ref_id)
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetContactObjective(ref_id, U.IsThai);
            ContactObjective[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<ContactObjective[]>(resultStr);
            return result;
        }

        public static ContentType[] GetContentType()
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetContentType(U.IsThai);
            ContentType[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<ContentType[]>(resultStr);
            return result;
        }

        public static Country[] GetCountry()
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetCountry(U.IsThai);
            Country[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<Country[]>(resultStr);
            return result;
        }

        public static District[] GetDistricts(string province_id)
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetDistricts(province_id, U.IsThai);
            District[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<District[]>(resultStr);
            return result;
        }

        public static DocumentType[] GetDocumentType()
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetDocumentType(U.IsThai);
            DocumentType[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<DocumentType[]>(resultStr);
            return result;
        }

        public static Gender[] GetGender()
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetGender(U.IsThai);
            Gender[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<Gender[]>(resultStr);
            return result;
        }

        public static MobileCaseObjective[] GetMobileCaseObjective()
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetMobileCaseObjective(U.IsThai);
            MobileCaseObjective[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<MobileCaseObjective[]>(resultStr);
            return result;
        }
        public static MobileContactObjective[] GetMobileContactObjective()
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetMobileContactObjective(U.IsThai);
            MobileContactObjective[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<MobileContactObjective[]>(resultStr);
            return result;
        }
        public static OperatingStatus[] GetOperatingStatus()
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetOperatingStatus(U.IsThai);
            OperatingStatus[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<OperatingStatus[]>(resultStr);
            return result;
        }

        public static OperatingObject[] GetOperatingObjective(string ref_id)
        {

            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetOperatingObjective(ref_id, U.IsThai);
            OperatingObject[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<OperatingObject[]>(resultStr);
            return result;
        }

        public static OperatingType[] GetOperatingType(string ref_id, bool is_cooperate = true)
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetOperatingType(ref_id, is_cooperate, U.IsThai);
            OperatingType[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<OperatingType[]>(resultStr);
            return result;
        }

        public static Organization[] GetOrganization(string ref_id)
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetOrganization(ref_id, U.IsThai);
            Organization[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<Organization[]>(resultStr);
            return result;
        }

        public static Priority[] GetPriority()
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetPriority(U.IsThai);
            Priority[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<Priority[]>(resultStr);
            return result;
        }

        public static Province[] GetProvinces()
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetProvinces(U.IsThai);
            Province[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<Province[]>(resultStr);
            return result;
        }

        public static Secret[] GetSecret()
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetSecret(U.IsThai);
            Secret[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<Secret[]>(resultStr);
            return result;
        }

        public static Severity[] GetSeverity()
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetSeverity(U.IsThai);
            Severity[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<Severity[]>(resultStr);
            return result;
        }

        public static SubDistrict[] GetSubDistricts(string district_id)
        {
            var s = new ListOfValuesSoapClient();
            var resultStr = s.GetSubDistricts(district_id, U.IsThai);
            SubDistrict[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<SubDistrict[]>(resultStr);
            return result;
        }

        #endregion


    }

    public static class WSA
    {
        #region ManageLOV
        public static string DeleteCaseObjective(string page_id, string id)
        {
            var s = new ServiceManageLOV.ManageLOVSoapClient();
            var resultStr = s.DeleteCaseObjective(U.CurrentUser.Token, page_id, id);
            return resultStr;
        }

        public static string DeleteCaseStatus(string page_id, string id)
        {
            var s = new ServiceManageLOV.ManageLOVSoapClient();
            var resultStr = s.DeleteCaseStatus(U.Token, page_id, id);
            return resultStr;
        }

        public static CaseObjective[] GetCaseObjective()
        {
            var s = new ServiceManageLOV.ManageLOVSoapClient();
            var resultStr = s.GetCaseObjective(U.Token, U.IsThai.ToString());
            CaseObjective[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<CaseObjective[]>(resultStr);
            return result;
        }

        ///xxxxxxxxxxxxxxxxxx เป็นส่วน Admin ยังมี service method อื่นๆ ให้เรียกใช้ได้มากกว่านี้

        #endregion

    }

    public static class WSM
    {
        #region Member
        public static string Activate(string member_id, string member_code)
        {
            var s = new MemberSoapClient();
            var resultStr = s.Activate(member_id, member_code);
            return resultStr;
        }

        public static string ActivateByCode(string email, string activate_code)
        {
            var s = new MemberSoapClient();
            var resultStr = s.ActivateByCode(email, activate_code);
            return resultStr;
        }

        public static string ChangePassword(string token_id, string old_password, string new_password)
        {
            var s = new MemberSoapClient();
            var resultStr = s.ChangePassword(token_id, old_password, new_password);
            return resultStr;
        }

        public static string CheckEmail(string email)
        {
            var s = new MemberSoapClient();
            var resultStr = s.CheckEmail(email);
            return resultStr;
        }

        public static string CheckOtherName(string other_name)
        {
            var s = new MemberSoapClient();
            var resultStr = s.CheckOtherName(other_name);
            return resultStr;
        }

        public static string GetAllProfile(string token_id)
        {
            var s = new MemberSoapClient();
            var resultStr = s.GetAllProfile(token_id);
            return resultStr;
        }

        public static string GetAttachment(string token_id, string attachment_id)
        {
            var s = new MemberSoapClient();
            var resultStr = s.GetAttachment(token_id, attachment_id);
            return resultStr;
        }

        public static string GetCase(string token_id, string case_id)
        {
            var s = new MemberSoapClient();
            var resultStr = s.GetCase(token_id, case_id);
            return resultStr;
        }

        public static string GetCaseAttachments(string token_id, string attachment_id)
        {
            var s = new MemberSoapClient();
            var resultStr = s.GetAttachment(token_id, attachment_id);
            return resultStr;
        }

        public static string GetCases(string token_id, string case_id)
        {
            var s = new MemberSoapClient();
            var resultStr = s.GetCase(token_id, case_id);
            return resultStr;
        }

        public static string GetContentDetail(string token_id, string content_id)
        {
            var s = new MemberSoapClient();
            var resultStr = s.GetContentDetail(token_id, content_id);
            return resultStr;
        }

        public static string GetContentHeaders(string token_id, int? skip = null, int? take = null)
        {
            skip = !skip.HasValue ? 0 : skip.Value;
            take = !take.HasValue ? U.RowPerPage : take.Value;
            var s = new MemberSoapClient();
            var resultStr = s.GetContentHeaders(token_id, skip.Value, take.Value);
            return resultStr;
        }

        public static string GetLastPassword(string token_id)
        {
            var s = new MemberSoapClient();
            var resultStr = s.GetLastPassword(token_id);
            return resultStr;
        }

        public static string GetPictureProfile(string token_id)
        {
            var s = new MemberSoapClient();
            var resultStr = s.GetPictureProfile(token_id);
            return resultStr;
        }

        public static string GetProfile(string token_id)
        {
            var s = new MemberSoapClient();
            var resultStr = s.GetProfile(token_id);
            return resultStr;
        }

        public static string GetState(string token_id)
        {
            var s = new MemberSoapClient();
            var resultStr = s.GetState(token_id);
            return resultStr;
        }

        public static string GetToken(string email, string password, AuthenForm authen_from, string ip_address)
        {
            var s = new MemberSoapClient();
            var resultStr = s.GetToken(email, password, authen_from.ToString(), ip_address);
            return resultStr;
        }

        public static string InformAttachment(string token_id, string case_id, string file_name, string file_content,
            string file_description)
        {
            var s = new MemberSoapClient();
            var resultStr = s.InformAttachment(token_id, case_id, file_name, file_content, file_description);
            return resultStr;
        }

        public static string InformCase(string token_id, string objective_id, string type_id, string date_opened,
            string channel_incoming_id,
            string call_back, string channel_outgoing_id, string summary, string date_occurrenced,
            string country_id, string case_area,
            string subdistrict_id, string district_id, string province_id, string postcode, string latitude,
            string longitude,
            string case_value, string is_secret, string used_inform_with, string defendent, string is_incourt, string contact_secret,
            string is_victim, string cust_type,
            string cust_firstname,
            string cust_lastname, string cust_citizen_id, string cust_contact_detail, string cust_country_id,
            string cust_number, string cust_address, string cust_subdistrict_id, string cust_district_id,
            string cust_province_id,
            string cust_postcode, string cust_is_public)
        {
            var s = new MemberSoapClient();
            var resultStr = s.InformCase(token_id, objective_id, type_id, date_opened, channel_incoming_id,
                call_back, channel_outgoing_id, summary, date_occurrenced, country_id, case_area,
                subdistrict_id, district_id, province_id, postcode, latitude, longitude,
                case_value, is_secret, used_inform_with, defendent, is_incourt, contact_secret, is_victim, cust_type, cust_firstname,
                cust_lastname, cust_citizen_id, cust_contact_detail, cust_country_id,
                cust_number, cust_address, cust_subdistrict_id, cust_district_id, cust_province_id,
                cust_postcode, cust_is_public);
            return resultStr;
        }

        public static string RecoveryPassword(string email, string citizen_id)
        {
            var s = new MemberSoapClient();
            var resultStr = s.RecoveryPassword(email, citizen_id);
            return resultStr;
        }

        public static string Register(string email, string firstname, string lastname, string citizen_id,
            string password, string country_id, string gender_id,
            string default_language)
        {
            var s = new MemberSoapClient();
            var resultStr = s.Register(email, firstname, lastname, citizen_id, password, country_id, gender_id,
                default_language);
            return resultStr;
        }

        public static string ReleaseToken(string token_id)
        {
            var s = new MemberSoapClient();
            var resultStr = s.ReleaseToken(token_id);
            return resultStr;
        }

        public static string SetPictureProfile(string token_id, string picture_name, string data_url)
        {
            var s = new MemberSoapClient();
            var resultStr = s.SetPictureProfile(token_id, picture_name, data_url);
            return resultStr;
        }

        public static string UpdateProfile(string token_id, string salutation_th, string firstname_th,
            string lastname_th,
            string salutation_en, string firstname_en, string lastname_en, string other_name, string gender_id,
            string citizen_id, string country_id, string birthday, string address, string subdistrict_id,
            string district_id, string province_id, string postcode, string mobile,
            string phone, string remark, string default_language)
        {
            var s = new MemberSoapClient();
            var resultStr = s.UpdateProfile(token_id, salutation_th, firstname_th, lastname_th,
                salutation_en, firstname_en, lastname_en, other_name, gender_id,
                citizen_id, country_id, birthday, address, subdistrict_id,
                district_id, province_id, postcode, mobile,
                phone, remark, default_language);
            return resultStr;
        }

        #endregion

    }

    public static class WSO
    {
        #region Service Officer

        public static CountEachState GetCountEachState(string token_id)
        {
            CountEachState result = new CountEachState();
            if (!string.IsNullOrWhiteSpace(token_id))
            {
                var s = new OfficerSoapClient();
                var resultStr = s.GetCountEachState(token_id);
                result = Newtonsoft.Json.JsonConvert.DeserializeObject<CountEachState>(resultStr);
            }
            return result;
        }
        public static string ChangePassword(string token_id, string old_password, string new_password)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.ChangePassword(token_id, old_password, new_password);
            return resultStr;
        }

        public static string CheckOtherName(string other_name)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.CheckOtherName(other_name);
            return resultStr;
        }

        public static string CheckUsername(string username)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.CheckUsername(username);
            return resultStr;
        }

        public static GetContact GetContact(string token_id, string operating_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetContact(token_id, operating_id);
            GetContact result = Newtonsoft.Json.JsonConvert.DeserializeObject<GetContact>(resultStr);
            return result;
        }

        public static GetOfficerProfileLogs[] GetOfficerProfileLogs(string token_id, string officer_id, int skip, int take)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetOfficerProfileLogs(token_id, officer_id, skip, take);
            GetOfficerProfileLogs[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<GetOfficerProfileLogs[]>(resultStr);
            return result;
        }

        public static string AddContact(string token_id, string case_id, string channel_id, string objective_id,
            string type_id,
            string status_id, string cont_start, string cont_end, string summary, string detail, string cont_is_secret,
            string account_id,
            string cont_type, string cont_gender_id, string cont_title, string cont_firstname, string cont_lastname,
            string cont_citizen_id, string cont_contact_detail, string cont_country_id, string cont_address,
            string cont_subdistrict_id, string cont_district_id, string cont_province_id, string cont_postcode,
            string cont_birthday, string cont_remark, string cont_is_defective, int cont_number, string cont_is_victim, string severity_id, string priority_id,
            string secret_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.AddContact(token_id, case_id, channel_id, objective_id, type_id,
                status_id, cont_start, cont_end, summary, detail, cont_is_secret, account_id,
                cont_type, cont_gender_id, cont_title, cont_firstname, cont_lastname,
                cont_citizen_id, cont_contact_detail, cont_country_id, cont_address,
                cont_subdistrict_id, cont_district_id, cont_province_id, cont_postcode,
                cont_birthday, cont_remark, cont_is_defective, cont_number, cont_is_victim,
                severity_id, priority_id, secret_id);
            return resultStr;
        }

        public static string AddCustomer(string token_id, string case_id, string cust_type, string cust_gender_id, string cust_title, string cust_firstname,
            string cust_lastname, string cust_citizen_id, string cust_contact_detail, string cust_country_id, int cust_number, string cust_address,
            string cust_subdistrict_id, string cust_district_id, string cust_province_id, string cust_postcode, string cust_birthday, string cust_remark,
            string cust_is_defective, string cust_is_public)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.AddCustomer(token_id, case_id, cust_type, cust_gender_id, cust_title, cust_firstname,
             cust_lastname, cust_citizen_id, cust_contact_detail, cust_country_id, cust_number, cust_address,
             cust_subdistrict_id, cust_district_id, cust_province_id, cust_postcode, cust_birthday, cust_remark,
             cust_is_defective, cust_is_public);
            return resultStr;
        }

        public static string AddOperating(string token_id, string case_id, string type_id, string status_id,
            string terminal_org_id, string terminal_owner_id,
            string date_opened, string date_closed, string summary, string detail, string severity_id,
            string priority_id, string secret_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.AddOperating(token_id, case_id, type_id, status_id, terminal_org_id, terminal_owner_id,
                date_opened, date_closed, summary, detail, severity_id, priority_id, secret_id);
            return resultStr;
        }

        public static string DuplicateCase(string token_id, string case_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.DuplicateCase(token_id, case_id);
            return resultStr;
        }

        public class Te
        {
            public Te(string k, object v1, object v2)
            {
                this.k = k;
                vHc = v1;
                vDa = v2;
            }
            public string k { get; set; }
            public object vHc { get; set; }
            public object vDa { get; set; }
            public override string ToString()
            {
                var t1 = vHc != null ? this.vHc.GetType() : typeof(string);
                var t2 = typeof(ArrayOfString);
                if (t1 != t2)
                {
                    return k.PadRight(32, ' ') + "=>\t" + (vHc ?? "(Null)").ToString().PadRight(32, ' ') + ",\t" + (vDa ?? "(Null)").ToString().PadRight(32, ' ');
                }

                string result = k + "=>\t" + string.Join(",", (vHc as ArrayOfString).ToArray()) + "=>\t" +
                          string.Join(",", (vDa as ArrayOfString).ToArray());
                return result;
            }
        }

        public static string EditCase(GetCase model)
        {
            var s = new OfficerSoapClient();

            if (model.list_case_org_owner.Any())
            {
                model.org_master_id = model.list_case_org_owner[0].org_id;
            }
            var data = model.list_case_org_owner.Select(o => o.org_id);

            ArrayOfString a = new ArrayOfString();
            a.AddRange(data);



            //model.country_id = "BE99E1FA21F2B35B7FF90DB29616E596";
            ////model.case_area = "";
            //model.subdistrict_id = "110602";
            //model.district_id = "1106";
            //model.province_id = "11";
            ////model.postcode = "12232";
            ////model.date_closed = null;
            //model.objective_id = "8";
            //model.type_id = "3";
            //model.priority_id = "3";
            //model.severity_id = "3";
            //model.secret_id = "3";
            //model.remark = model.remark;
            //model.defendant = model.defendant;
            //model.date_closed = DateTime.Now;
            //a = new ArrayOfString { "112" };




            List<Te> tes = new List<Te>();

            //var pw = new ServiceGeneral.GeneralSoapClient().EncodeString("");
            //var tokent = new ServiceOfficer.OfficerSoapClient().GetToken("dev", pw, "S", "::1");
            //var result = new ServiceOfficer.OfficerSoapClient().EditCase(
            /*<token_id>D26C8E298E1B41D694FE06E79CA78329</token_id>
  <case_id>F1E4E0D9DCCF42A18EED6AB61EE0591F</case_id>
  <objective_id>8</objective_id>
  <type_id>3</type_id>
  <terminal_type_id />*/
            tes.AddRange(new List<Te>
            {
                new Te("token_id", U.Token, U.Token), //  tokent,

                new Te("case_id", "F1E4E0D9DCCF42A18EED6AB61EE0591F", model.case_id),
                new Te("objective_id", "8", model.objective_id),
                new Te("type_id", "3", model.type_id),
                new Te("terminal_type_id", "", model.terminal_type_id),
                /*<status_id>13</status_id>
      <date_opened>2015-05-23 00:00:00</date_opened>
      <date_closed>2015-05-23 00:00:00</date_closed>
      <channel_incoming_id>2</channel_incoming_id>
      <call_back>F</call_back>*/
                new Te("status_id", "13", model.status_id),
                new Te("date_opened", "2015-05-23", Util.ConvertDateTimeToWebService(model.date_opened)),
                new Te("date_closed", "2015-05-23", Util.ConvertDateTimeToWebService(model.date_closed)),
                new Te("channel_incoming_id", "2", model.channel_incoming_id),
                new Te("call_back", "F", model.call_back),
                /*<channel_outgoing_id />
      <summary>teswt</summary>
      <detail>setste</detail>
      <defendent />
      <date_occurrenced_from>2015-05-23 00:00:00</date_occurrenced_from>*/
                new Te("channel_outgoing_id", "", model.channel_outgoing_id),
                new Te("summary", "tste", model.summary),
                new Te("detail", "wawdsadf", model.detail),
                new Te("defendent", "", model.defendant),
                new Te("date_occurrenced_from", "2015-05-23", Util.ConvertDateTimeToWebService(model.date_occurrenced_from)),
                /*
      <date_occurrenced_to>2015-05-23 00:00:00</date_occurrenced_to>
      <time_occurrenced>8.00</time_occurrenced>
      <country_id>BE99E1FA21F2B35B7FF90DB29616E596</country_id>
      <case_area />
      <subdistrict_id>110602</subdistrict_id>*/
                new Te("date_occurrenced_to", "2015-05-23", Util.ConvertDateTimeToWebService(model.date_occurrenced_to)),
                new Te("time_occurrenced", "8.00", model.time_occurrenced),
                new Te("country_id", "BE99E1FA21F2B35B7FF90DB29616E596", model.country_id),
                new Te("case_area", "", model.case_area),
                new Te("subdistrict_id", "110602", model.subdistrict_id),
                /*
      <district_id>1106</district_id>
      <province_id>11</province_id>
      <postcode>12321</postcode>
      <latitude />
      <longitude />
       */
                new Te("district_id", "1106", model.district_id),
                new Te("province_id", "11", model.province_id),
                new Te("postcode", "12232", model.postcode),
                new Te("latitude", "", model.latitude),
                new Te("longitude", "", model.longitude),
                /*<case_value>0</case_value>
      <is_in_court>F</is_in_court>
      <remark />
      <is_secret>F</is_secret>
      <priority_id>3</priority_id>*/
                new Te("case_value", "0", model.case_value),
                new Te("is_in_court", "F", model.is_in_court),
                new Te("remark", "fasdf asd", model.remark),
                new Te("is_secret", "F", model.is_secret),
                new Te("priority_id", "3", model.priority_id),
                /* <severity_id>3</severity_id>
      <secret_id>3</secret_id>
      <org_master_id>1</org_master_id>*/
                new Te("severity_id", "3", model.severity_id),
                new Te("secret_id", "3", model.secret_id),
                new Te("org_master_id", "1", model.org_master_id),
                new Te("org_id", new ArrayOfString {"112"}, a)
            });


            var x = string.Join("\r\n", tes);
            string resultStr = "";

            try
            {
                resultStr = s.EditCase(U.Token,
                    model.case_id,
                    model.objective_id,
                    model.type_id,
                    model.terminal_type_id,

                    model.status_id,
                    Util.ConvertDateTimeToWebService(model.date_opened),
                    Util.ConvertDateTimeToWebService(model.date_closed),
                    model.channel_incoming_id,
                    model.call_back,
                    model.channel_outgoing_id,
                    model.summary,
                    model.detail,
                    model.defendant,
                    Util.ConvertDateTimeToWebService(model.date_occurrenced_from),
                    Util.ConvertDateTimeToWebService(model.date_occurrenced_to),
                    model.time_occurrenced,
                    model.country_id,
                    model.case_area,
                    model.subdistrict_id,
                    model.district_id,
                    model.province_id,
                    model.postcode,
                    model.latitude,
                    model.longitude,
                    Convert.ToString(model.case_value),
                    model.is_in_court,
                    model.remark,
                    model.is_secret,
                    model.priority_id,
                    model.severity_id,
                    model.secret_id,
                    model.org_master_id,
                    a);
            }
            catch (Exception ex)
            {
                throw new Exception(x + ", " + resultStr, ex);
            }
            return resultStr;
        }

        public static string EditContactAccount(string token_id, string account_id, string cust_type, string cust_gender_id, string cust_title, string cust_firstname,
            string cust_lastname, string cust_citizen_id, string cust_contact_detail, string cust_country_id, int cust_number, string cust_address, string cust_subdistrict_id,
            string cust_district_id, string cust_province_id, string cust_postcode, string cust_birthday, string cust_remark, string cust_is_defective, string cust_is_public)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.EditContactAccount(token_id, account_id, cust_type, cust_gender_id, cust_title, cust_firstname,
             cust_lastname, cust_citizen_id, cust_contact_detail, cust_country_id, cust_number, cust_address, cust_subdistrict_id,
             cust_district_id, cust_province_id, cust_postcode, cust_birthday, cust_remark, cust_is_defective, cust_is_public);
            return resultStr;
        }

        public static string EditContact(string token_id, string case_id, string operating_id, string contact_id,
            string channel_id, string objective_id, string type_id, string status_id, string cont_start, string cont_end,
            string cont_is_secret, string account_id, string summary, string detail, string cont_is_victim,
            string severity_id, string priority_id, string secret_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.EditContact(token_id, case_id, operating_id, contact_id,
                channel_id, objective_id, type_id, status_id, cont_start, cont_end,
                cont_is_secret, account_id, summary, detail, cont_is_victim,
                severity_id, priority_id, secret_id);
            return resultStr;
        }

        public static string EditOperating(string token_id, string operating_id, string type_id, string status_id,
            string terminal_org_id,
            string terminal_owner_id, string date_opened, string date_closed, string summary, string detail,
            string severity_id,
            string priority_id, string secret_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.EditOperating(token_id, operating_id, type_id, status_id, terminal_org_id,
                terminal_owner_id, date_opened, date_closed, summary, detail, severity_id,
                priority_id, secret_id);
            return resultStr;
        }

        public static string EditCustomerProfile(string token_id, string customer_id, string cont_is_secret, string cont_type, string cont_gender_id, string cont_title, string cont_firstname,
            string cont_lastname, string cont_citizen_id, string cont_contact_detail, string cont_country_id, string cont_address, string cont_subdistrict_id, string cont_district_id,
            string cont_province_id, string cont_postcode, string cont_birthday, string cont_remark, int cont_number, string cont_is_defective)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.EditCustomerProfile(token_id, customer_id, cont_is_secret, cont_type, cont_gender_id, cont_title, cont_firstname,
             cont_lastname, cont_citizen_id, cont_contact_detail, cont_country_id, cont_address, cont_subdistrict_id, cont_district_id,
             cont_province_id, cont_postcode, cont_birthday, cont_remark, cont_number, cont_is_defective);
            return resultStr;
        }

        public static OfficerProfile GetAllProfile()
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetAllProfile(U.Token);
            OfficerProfile result = Newtonsoft.Json.JsonConvert.DeserializeObject<OfficerProfile>(resultStr);
            return result;
        }

        public static OfficerProfile UpdateProfile(string token_id, string gender_id, string salutation_th, string firstname_th, string lastname_th, string salutation_en,
            string firstname_en, string lastname_en, string other_name, string birthday, string citizen_id, string mobile, string phone, string email,
            string position, string role_id, string org_id, string address, string subdistrict_id, string district_id, string province_id, string postcode,
            string default_language, int row_per_page, string picture_profile, string picture_name, string is_notify, string commander_id)
        {

            var s = new OfficerSoapClient();
            var resultStr = s.UpdateProfile(token_id, gender_id, salutation_th, firstname_th, lastname_th, salutation_en,
             firstname_en, lastname_en, other_name, birthday, citizen_id, mobile, phone, email,
             position, role_id, org_id, address, subdistrict_id, district_id, province_id, postcode,
             default_language, row_per_page, picture_profile, picture_name, is_notify, commander_id);
            OfficerProfile result = Newtonsoft.Json.JsonConvert.DeserializeObject<OfficerProfile>(resultStr);

            return result;
        }

        public static GetCase GetCase(string token_id, string case_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetCase(U.Token, case_id);
            GetCase result = Newtonsoft.Json.JsonConvert.DeserializeObject<GetCase>(resultStr);
            return result;
        }

        public static string GetCaseLogs(string token_id, string case_id, int skip, int take)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetCaseLogs(U.Token, case_id, skip, take);
            return resultStr;
        }

        public static OfficerProfile[] GetCaseOfficer(string token_id, string case_id, string org_id, int skip, int take)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetCaseOfficer(U.Token, case_id, org_id, skip, take);
            OfficerProfile[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<OfficerProfile[]>(resultStr);
            return result;
        }

        public static string GetCaseOrganization(string token_id, string case_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetCaseOrganization(U.Token, case_id);
            return resultStr;
        }

        public static string GetPictureProfile(string token_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = Newtonsoft.Json.JsonConvert.DeserializeObject(s.GetPictureProfile(U.Token)).ToString();
            return resultStr;
        }

        public static User GetProfile(GetTokenResult tokenobj)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetProfile(tokenobj.token_id);
            User result = Newtonsoft.Json.JsonConvert.DeserializeObject<User>(resultStr);
            result.GetTokenResult = tokenobj;
            return result;
        }

        public static ContactAccount GetContactAccount(string token_id, string account_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetContactAccount(U.Token, account_id);
            ContactAccount result = Newtonsoft.Json.JsonConvert.DeserializeObject<ContactAccount>(resultStr);
            return result;
        }

        public static CaseCustomerProfile[] GetCaseCustomerProfile(string token_id, string case_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetCaseCustomerProfile(U.Token, case_id);
            CaseCustomerProfile[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<CaseCustomerProfile[]>(resultStr);
            return result;
        }

        public static ContactAccountCases[] GetContactAccountCases(string token_id, string account_id, int skip, int take)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetContactAccountCases(U.Token, account_id, skip, take);
            ContactAccountCases[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<ContactAccountCases[]>(resultStr);
            return result;
        }

        public static string GetContactAccountLogs(string token_id, string account_id, int skip, int take)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetContactAccountLogs(U.Token, account_id, skip, take);
            return resultStr;
        }

        public static string GetCustomerAddress(string token_id, string customer_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetCustomerAddress(U.Token, customer_id);
            return resultStr;
        }

        public static string GetCustomerProfile(string token_id, string customer_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetCustomerProfile(U.Token, customer_id);
            return resultStr;
        }

        public static string GetCustomerProfileCases(string token_id, string customer_id, int skip, int take)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetCustomerProfileCases(U.Token, customer_id, skip, take);
            return resultStr;
        }

        public static string GetCustomerProfileLogs(string token_id, string profile_id, int skip, int take)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetCustomerProfileLogs(U.Token, profile_id, skip, take);
            return resultStr;
        }

        public static Groups[] GetGroups(string token_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetGroups(U.Token);
            Groups[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<Groups[]>(resultStr);
            return result;
        }

        public static GetKnowledgeComments[] GetKnowledgeComments(string token_id, string knowledge_id, int skip, int take)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetKnowledgeComments(U.Token, knowledge_id, skip, take);
            GetKnowledgeComments[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<GetKnowledgeComments[]>(resultStr);
            return result;
        }

        public static GetKnowledgeDetail GetKnowledgeDetail(string token_id, string knowledge_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetKnowledgeDetail(U.Token, knowledge_id);
            GetKnowledgeDetail result = Newtonsoft.Json.JsonConvert.DeserializeObject<GetKnowledgeDetail>(resultStr);
            return result;
        }

        public static GetKnowledges GetKnowledgeSmile(string token_id, string knowledge_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetKnowledgeSmile(U.Token, knowledge_id);
            GetKnowledges result = Newtonsoft.Json.JsonConvert.DeserializeObject<GetKnowledges>(resultStr);
            return result;
        }

        public static string SetKnowledgeSmile(string token_id, string content_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.SetKnowledgeSmile(U.Token, content_id);
            return resultStr;
        }

        public static string GetReadKnowledge(string token_id, string content_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetReadKnowledge(U.Token, content_id);
            return resultStr;
        }

        public static GetKnowledges[] GetKnowledges(string token_id, string type_id, int skip, int take)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetKnowledges(U.Token, type_id, skip, take);
            GetKnowledges[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<GetKnowledges[]>(resultStr);
            return result;
        }

        public static string AddKnowledgeComment(string token_id, string content_id, string comment_text, string ref_comment_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.AddKnowledgeComment(U.Token, content_id, comment_text, ref_comment_id);
            return resultStr;
        }

        public static string EditKnowledgeComment(string token_id, string comment_id, string content_id, string comment_text, string ref_comment_id = "")
        {
            var s = new OfficerSoapClient();
            var resultStr = s.EditKnowledgeComment(U.Token, comment_id, content_id, comment_text, ref_comment_id);
            return resultStr;
        }

        public static GetMessage[] GetNews(string token_id)
        {
            GetMessage[] result = new GetMessage[0];
            if (!string.IsNullOrWhiteSpace(token_id))
            {
                var s = new OfficerSoapClient();
                var resultStr = s.GetNews(U.Token);
                result = Newtonsoft.Json.JsonConvert.DeserializeObject<GetMessage[]>(resultStr);
            }
            return result;
        }

        public static GetMessage GetNewsDetail(string token_id, string news_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetNewsDetail(U.Token, news_id);
            GetMessage result = Newtonsoft.Json.JsonConvert.DeserializeObject<GetMessage>(resultStr);
            return result;
        }

        public static Notification[] GetNotifies(string token_id)
        {
            if (string.IsNullOrWhiteSpace(token_id)) return new Notification[0];
            var s = new OfficerSoapClient();
            var resultStr = s.GetNotifies(token_id);
            Notification[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<Notification[]>(resultStr);
            return result;
        }

        public static OfficerProfile GetOfficerProfile(string token_id, string officer_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetOfficerProfile(U.Token, officer_id);
            OfficerProfile result = Newtonsoft.Json.JsonConvert.DeserializeObject<OfficerProfile>(resultStr);
            return result;
        }

        public static GetOperating GetOperating(string token_id, string operating_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetOperating(U.Token, operating_id);
            GetOperating result = Newtonsoft.Json.JsonConvert.DeserializeObject<GetOperating>(resultStr);
            return result;
        }

        public static GetOperatings[] GetOperatings(string token_id, string case_id, string select_org_id, int skip, int take)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetOperatings(U.Token, case_id, select_org_id, skip, take);
            GetOperatings[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<GetOperatings[]>(resultStr);
            return result;
        }

        public static string GetOrganizationAddress(string token_id, string org_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetOrganizationAddress(U.Token, org_id);
            return resultStr;
        }

        public static GetTimelineHeader[] GetTimelineHeader(string token_id, string last_get_date_time, string timeline_type = "A",
            int skip = 0, int take = 10)
        {
            GetTimelineHeader[] result = new GetTimelineHeader[0];

            if (!string.IsNullOrWhiteSpace(token_id))
            {
                var s = new OfficerSoapClient();
                var resultStr = s.GetTimelineHeader(token_id, last_get_date_time, timeline_type, skip, take);
                result = Newtonsoft.Json.JsonConvert.DeserializeObject<GetTimelineHeader[]>(resultStr);
            }
            return result;
        }

        public static TimelineOperating[] GetTimelineOperating(string token_id, string cast_id, int skip, int take)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetTimelineOperating(token_id, cast_id, skip, take);
            TimelineOperating[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<TimelineOperating[]>(resultStr);
            return result;
        }

        public static GetTokenResult GetToken(string user, string password, AuthenForm authen_from, string ip_address)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.GetToken(user, password, authen_from.ToString(), ip_address);
            GetTokenResult result = !resultStr.StartsWith("\"Err")
                ? Newtonsoft.Json.JsonConvert.DeserializeObject<GetTokenResult>(resultStr)
                : null;
            return result;
        }

        public static string InformAttachment(string token_id, string case_id,
            string file_name, string file_content, string file_description,
            string doc_type_id, string doc_type, string doc_no, string doc_date,
            string doc_ref_no, string doc_ref_date)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.InformAttachment(token_id, case_id, file_name, file_content, file_description,
                doc_type_id, doc_type, doc_no, doc_date, doc_ref_no, doc_ref_date);

            return resultStr;
        }

        public static string InformCase(string token_id, string objective_id, string type_id,
            string terminal_type_id, string status_id,
            string date_opened, string date_closed, string channel_incoming_id, string call_back,
            string channel_outgoing_id, string summary, string detail, string defendent, string used_inform_with,
            string date_occurrenced_from, string date_occurrenced_to, string time_occurrenced, string country_id,
            string case_area, string subdistrict_id, string district_id, string province_id,
            string postcode, string latitude, string longitude, string case_value,
            string is_in_court, string remark, string is_secret, string priority_id,
            string severity_id, string secret_id, string org_master_id, OPM.ServiceOfficer.ArrayOfString org_id,
            string cont_is_secret, string cont_type, string cont_gender_id, string cont_title,
            string cont_firstname, string cont_lastname, string cont_citizen_id, string cont_contact_detail,
            string cont_country_id, string cont_address, string cont_subdistrict_id, string cont_district_id,
            string cont_province_id, string cont_postcode, string cont_birthday, string cont_remark, int cont_number, string cont_is_defective,
            string cont_is_victim, string cust_type, string cust_gender_id, string cust_title,
            string cust_firstname, string cust_lastname, string cust_citizen_id, string cust_contact_detail,
            string cust_country_id, int cust_number, string cust_address, string cust_subdistrict_id,
            string cust_district_id, string cust_province_id, string cust_postcode, string cust_birthday,
            string cust_remark, string cust_is_defective, string cust_is_public)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.InformCase(token_id, objective_id, type_id, terminal_type_id, status_id,
                date_opened, date_closed, channel_incoming_id, call_back,
                channel_outgoing_id, summary, detail, defendent, used_inform_with,
                date_occurrenced_from, date_occurrenced_to, time_occurrenced, country_id, case_area,
                subdistrict_id, district_id, province_id, postcode,
                latitude, longitude, case_value, is_in_court,
                remark, is_secret, priority_id, severity_id,
                secret_id, org_master_id, org_id, cont_is_secret, cont_type,
                cont_gender_id, cont_title, cont_firstname, cont_lastname,
                cont_citizen_id, cont_contact_detail, cont_country_id, cont_address,
                cont_subdistrict_id, cont_district_id, cont_province_id, cont_postcode,
                cont_birthday, cont_remark, cont_number, cont_is_defective, cont_is_victim, cust_type,
                cust_gender_id, cust_title, cust_firstname, cust_lastname,
                cust_citizen_id, cust_contact_detail, cust_country_id, cust_number,
                cust_address, cust_subdistrict_id, cust_district_id, cust_province_id,
                cust_postcode, cust_birthday, cust_remark, cust_is_defective, cust_is_public);
            return resultStr;
        }

        public static string OperatingAttachment(string token_id, string case_id,
            string operating_id, string file_name, string file_content,
            string file_description, string doc_type_id, string doc_type, string doc_no,
            string doc_date, string doc_ref_no, string doc_ref_date)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.OperatingAttachment(token_id, case_id, operating_id, file_name, file_content,
                file_description, doc_type_id, doc_type, doc_no, doc_date, doc_ref_no, doc_ref_date);
            return resultStr;
        }

        public static string RecoveryPassword(string username, string citizen_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.RecoveryPassword(username, citizen_id);
            return resultStr;
        }

        public static string ReleaseToken(string token_id, string picture_name, string data_url)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.ReleaseToken(U.Token);
            return resultStr;
        }

        public static SearchContact[] SearchContactAccounts(string token_id, string keyword, int skip, int take)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.SearchContactAccounts(token_id, keyword, skip, take);
            SearchContact[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<SearchContact[]>(resultStr);
            return result;
        }

        public static SearchCustomer[] SearchCustomerProfiles(string token_id, string keyword, int skip, int take)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.SearchCustomerProfiles(U.Token, keyword, skip, take);
            SearchCustomer[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<SearchCustomer[]>(resultStr);
            return result;
        }

        public static string SearchTempCases(string token_id, string keyword, string temp_code, string date_from, string date_to, int skip, int take)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.SearchTempCases(U.Token, keyword, temp_code, date_from, date_to, skip, take);
            return resultStr;
        }

        public static string SetCloseTempCase(string token_id, string case_id)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.SetCloseTempCase(U.Token, case_id);
            return resultStr;
        }

        public static string SetPictureProfile(string token_id, string picture_name, string data_url)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.SetPictureProfile(U.Token, picture_name, data_url);
            return resultStr;
        }

        public static string SearchCases(string token_id, string keyword, int skip, int take)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.SearchCases(U.Token, keyword, skip, take);
            return resultStr;
        }

        public static SearchCases[] SearchCasesWithTQ(string token_id, string keyword, int skip, int take)
        {
            var s = new OfficerSoapClient();
            var resultStr = s.SearchCasesWithTQ(U.Token, keyword, skip, take);
            SearchCases[] result = Newtonsoft.Json.JsonConvert.DeserializeObject<SearchCases[]>(resultStr);
            return result;
        }

        #endregion
    }
}